package com.xinsoft.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.entity.dto.SysDictTypeParam;
import com.xinsoft.entity.po.SysDictionary;
import com.xinsoft.entity.po.SysDictionaryType;
import com.xinsoft.mapper.SysDictionaryMapper;
import com.xinsoft.service.SysDictionaryService;
import com.xinsoft.service.SysDictionaryTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lijie
 * @description 针对表【sys_dictionary(字典表)】的数据库操作Service实现
 * @createDate 2023-05-16 17:10:50
 */
@Service
public class SysDictionaryServiceImpl extends ServiceImpl<SysDictionaryMapper, SysDictionary>
        implements SysDictionaryService {

    @Resource
    SysDictionaryMapper dictionaryMapper;

    @Resource
    SysDictionaryTypeService typeService;
    @Override
    public Object findPageListByParam(SysDictTypeParam param) {
        // 分页
        Page<SysDictionary> page = new Page<SysDictionary>(param.getCurrentPage(), param.getPageSize());
        LambdaQueryWrapper<SysDictionary> queryWrapper = new LambdaQueryWrapper<>();
        // 查询条件
        queryWrapper
                .eq(param.getTypeId() != null, SysDictionary::getDictionaryTypeId, param.getTypeId())
                .like(StrUtil.isNotBlank(param.getDictName()),SysDictionary::getDicName, param.getDictName());
        IPage<SysDictionary> resultPage = dictionaryMapper.selectPageInfo(page, queryWrapper,param.getTypeName(),param.getDoUpdate());
        // 返回值组装下字典组名
        return resultPage;
    }

    @Override
    public List<SysDictionary> getDictByTypeCode(String typeCode) {
        List<SysDictionary> list = new ArrayList<>();
        QueryWrapper<SysDictionary> queryWrapper = new QueryWrapper<>();
        // 获取字典类型对象
        SysDictionaryType type = typeService.getOne(new QueryWrapper<SysDictionaryType>().eq("type_code",typeCode));
        if(type != null){
            //查询条件
            queryWrapper
                    .eq("dictionary_type_id",type.getId())
                    .orderByAsc("dic_sort");
            list = dictionaryMapper.selectList(queryWrapper);
        }
        return list;
    }
}




